package cn.jinjimi.app.search;

import cn.jinjimi.app.entity.Company;

import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by lforxeverc on 16/7/27.
 */
public class CompanyProvider extends BaseProvider {
    List<Company> mCompanies;

    int mCustomCount = 0;
    public CompanyProvider(SearchResultListener listener,List<Company> list) {
        super(listener);
        mCompanies = list;
    }

    @Override
    public int doQuery(String key, int startIndex) {
        if (mCancelQuery)
            return QUERY_STATE_CANCEL;

        if (mCompanies == null || startIndex == 0) {
//            mCompanies = CustomsManager.getInstance().getCustomsList();
            mCustomCount = mCompanies.size();
            if (mCancelQuery)
                return QUERY_STATE_CANCEL;
        }

        if (mCompanies == null)
            return QUERY_STATE_FAIL;

        if (mCompanies.isEmpty())
            return QUERY_STATE_OK;

        key = key.toUpperCase();

        int keyWordLen = key.length();

        for (int i = startIndex; i < mCustomCount; i++) {
            if (STOP_QUERY)
                return QUERY_STATE_OK;

            Company custom = mCompanies.get(i);

            String name = custom.getCompanyName().toUpperCase();
            int index = name.indexOf(key);
            if (index > -1) {
                mHasNextPager = i < mCustomCount - 1;//是否还有下一页
                CompanySearchRecord record = new CompanySearchRecord();
                record.index = index;
                record.length = keyWordLen;
                record.company = custom;
                addResult(record);
                continue;
            }

            toCompareChineseNamePinyin(custom, key, i);

        }
        mHasNextPager = false;
        addResult(null);

        return QUERY_STATE_OK;
    }

    Pattern mPatterm = Pattern.compile("^[A-Z]+$");

    private void toCompareChineseNamePinyin(Company company, String key, int position) {

    }
}
